import cv2 as cv
import json
from utils import *
from xml.dom.minidom import parse
def show_with_r(path,p1_x,p1_y,p2_x,p2_y):#用opencv显示一张图片，带有一个矩形
    img=cv.imread(path)
    cv.rectangle(img,(p1_x,p1_y), (p2_x,p2_y), (0, 0, 255), 1)
    cv.imshow("ddd",img)
    cv.waitKey(0)
def get_img_hw(path):
    img=cv.imread(path)
    return img.shape[:2]
def show_with_rs(path,p_list):##用opencv显示一张图片，带有n个矩形，p_list中每一个元素都由p1_x,p1_y,p2_x,p2_y
    img=cv.imread(path)
    for p in p_list:
        cv.rectangle(img,(p[0],p[1]), (p[2],p[3]), (0, 0, 255), 1)
    cv.imshow("ddd",img)
    cv.waitKey(0)

# 处理json和xml,得到自定义数据类型：
# [
# 	{cate1：[p1_x,p1_y,p2_x,p2_y]}，
# 	{cate1：[p1_x,p1_y,p2_x,p2_y]}，
# 	{cate1：[p1_x,p1_y,p2_x,p2_y]}，
# 	{cate1：[p1_x,p1_y,p2_x,p2_y]}
# ]
def coco2mydata(json_path):
    with open(json_path, 'r')as f:
        a = json.loads(f.read())
    json_all_data=[]
    for i in range(len(a["shapes"])):
        json_all_data.append({
            a["shapes"][i]["label"]:[int(a["shapes"][i]["points"][0][0]),int(a["shapes"][i]["points"][0][1]),
                                    int(a["shapes"][i]["points"][1][0]),int(a["shapes"][i]["points"][1][1])]
        })
    height,width=a["imageHeight"],a["imageWidth"]
    return json_all_data,height,width
def voc2mydata(xml_path):
    dom = parse(xml_path)
    elem = dom.documentElement
    names = elem.getElementsByTagName('name')
    x_mins,y_mins,x_maxs,y_maxs=[],[],[],[]
    x_mins= elem.getElementsByTagName('xmin')
    y_mins= elem.getElementsByTagName('ymin')
    x_maxs= elem.getElementsByTagName('xmax')
    y_maxs= elem.getElementsByTagName('ymax')
    for i in range(len(names)):
        names[i]=names[i].childNodes[0].nodeValue
        x_mins[i]=int(x_mins[i].childNodes[0].nodeValue)
        y_mins[i]=int(y_mins[i].childNodes[0].nodeValue)
        x_maxs[i]=int(x_maxs[i].childNodes[0].nodeValue)
        y_maxs[i]=int(y_maxs[i].childNodes[0].nodeValue)
    xml_all_data=[]
    for i in range(len(x_mins)):
        xml_all_data.append({names[i]:[x_mins[i],y_mins[i],x_maxs[i],y_maxs[i]]})
    height = int(elem.getElementsByTagName('height')[0].childNodes[0].nodeValue)
    width = int(elem.getElementsByTagName('width')[0].childNodes[0].nodeValue)
    return xml_all_data,height,width
def get_all_cate_str(cate_path):
    with open(cate_path,"r")as f:
        all_cate_str=f.read()
    return all_cate_str
def dress_mydata(my_data,all_cata_str,height,width):
    # print(my_data)
    yolo_all_data=[]
    for i in range(len(my_data)):
        a=list(my_data[i].values())[0]
        b=[((a[0]+a[2])/2)/width,((a[1]+a[3])/2)/height,(a[2]-a[0])/width,(a[3]-a[1])/height]
        yolo_all_data.append({list(my_data[i].keys())[0]:b})
    #print(yolo_all_data)#[{'eyes': [0.162098, 0.424051, 0.121928, 0.109]}, {'eyes': [0.160681, 0.631646, 0.066163, 0.104]}, {'eyes': [0.566163, 0.406329, 0.075614, 0.089]}, {'eyes': [0.577032, 0.589873, 0.038752, 0.091]}, {'eyes': [0.749055, 0.408861, 0.029301, 0.078]}, {'eyes': [0.743384, 0.593671, 0.029301, 0.109]}]


    #获取所有类别
    all_cata_list=all_cata_str.split("\n")[:-1]#['eyes', 'mouse']
    all_cata_dict={}
    for i in range(len(all_cata_list)):
        all_cata_dict[all_cata_list[i]]=str(i)
    # print(all_cata_dict)#{'eyes': '0', 'mouse': '1'}

    #生成yolo标注文件
    content=""
    for i in range(len(yolo_all_data)):
        content+=all_cata_dict[list(yolo_all_data[i].keys())[0]]+" "+\
            "%.6f" %(list(yolo_all_data[i].values())[0][0])+" "+\
            "%.6f" %(list(yolo_all_data[i].values())[0][1])+" "+\
            "%.6f" %(list(yolo_all_data[i].values())[0][2])+" "+\
            "%.6f" %(list(yolo_all_data[i].values())[0][3])+"\n"
    return content






